import Taro from "@tarojs/taro";
import getBaseUrl from "./baseUrl";
import interceptors from "./interceptors";
import  {RequestParams} from "./model"

interceptors.forEach((interceptorItem) => Taro.addInterceptor(interceptorItem));

class httpRequest {
  baseOptions(params:RequestParams,method) {
    const BASE_URL = getBaseUrl();
    // let contentType = "application/x-www-form-urlencoded";
    let contentType = "application/json;charset=UTF-8";
    contentType = params.contentType || contentType;
    const option = {
      url: BASE_URL +params.url, //地址
      data: params.data, //传参
      method:method, //请求方式
      timeout: 50000, // 超时时间
      header: {
        //请求头
        "content-type": contentType,
        // Authorization: Taro.getStorageSync("Authorization") || "",
      },
    };
    return Taro.request(option);
  }

  get(params:RequestParams) {
    return this.baseOptions(params,"GET");
  }

  post(params:RequestParams) {
    return this.baseOptions(params,"POST");
  }
}

export default new httpRequest();
